package mvp.com.bigimage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;

/**
 * 选择显示图片的区域
 */
public class MainActivity extends AppCompatActivity {

    private ImageView img_one;
    private ImageView img_two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img_one = (ImageView) findViewById(R.id.img_one);
        img_two = (ImageView) findViewById(R.id.img_two);

        InputStream open = null;
        try {
            open = getAssets().open("image_guide.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap1 = BitmapFactory.decodeStream(open);
        img_one.setImageBitmap(bitmap1);

        InputStream is;
        try {
            is = this.getAssets().open("image_guide.jpg");

            // 获取图片的宽和高
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, opt);
            int width = opt.outWidth;
            int height = opt.outHeight;

            // 设置图片的中心区域
            BitmapRegionDecoder regionDecoder = BitmapRegionDecoder.newInstance(is, false);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap bitmap = regionDecoder.decodeRegion(new Rect(width / 2 - 100, height / 2 - 100, width / 2 + 100, height / 2 + 100), options);
            img_two.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void go(View view) {
        startActivity(new Intent(this, TwoActivity.class));
    }
}
